﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace CrmContacto.Contracts.Config
{
    public class CustomerServiceHookConfig : ConfigurationSection
    {
        [ConfigurationProperty("CustomerServiceHookProviders", IsDefaultCollection = true)]
        [ConfigurationCollection(typeof(CustomerServiceHookProviderCollection),
         AddItemName = "add",
         ClearItemsName = "clear",
         RemoveItemName = "remove")]
        public CustomerServiceHookProviderCollection CustomerServiceHooks
        {
            get
            {
                return (CustomerServiceHookProviderCollection)base["CustomerServiceHookProviders"];
            }
        }
    }

    public class CustomerServiceHookProviderCollection : ConfigurationElementCollection
    {
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        public CustomerServiceHookProviderElement this[int index]
        {
            get { return (CustomerServiceHookProviderElement)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }

        public void Add(CustomerServiceHookProviderElement element)
        {
            BaseAdd(element);
        }

        public void Clear()
        {
            BaseClear();
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new CustomerServiceHookProviderElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((CustomerServiceHookProviderElement)element).Provider;
        }

        public void Remove(CustomerServiceHookProviderElement element)
        {
            BaseRemove(element.Provider);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }
    }

    public class CustomerServiceHookProviderElement : ConfigurationElement
    {
        [ConfigurationProperty("Provider", IsRequired = true, IsKey = true)]
        public string Provider
        {
            get { return (string)this["Provider"]; }
            set { this["Provider"] = value; }
        }

        [ConfigurationProperty("EmpresaId", IsRequired = true, IsKey = true)]
        public string EmpresaId
        {
            get { return (string)this["EmpresaId"]; }
            set { this["EmpresaId"] = value; }
        }

        [ConfigurationProperty("ConfigurationString", IsRequired = true, IsKey = true)]
        public string ConfigurationString
        {
            get { return (string)this["ConfigurationString"]; }
            set { this["ConfigurationString"] = value; }
        }

        [ConfigurationProperty("AssemblyFile", IsRequired = true, IsKey = true)]
        public string AssemblyFile
        {
            get { return (string)this["AssemblyFile"]; }
            set { this["AssemblyFile"] = value; }
        }
    }
}
